package com.yy.QSManage.service.impl;

import cn.hutool.core.util.ReflectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yy.QSManage.dao.FaSysOptionMapper;
import com.yy.QSManage.exception.ServiceException;
import com.yy.QSManage.model.dto.OptionsDto;
import com.yy.QSManage.model.entity.FaSysDataSource;
import com.yy.QSManage.model.entity.FaSysEnum;
import com.yy.QSManage.model.entity.FaSysOption;
import com.yy.QSManage.service.FaSysDataSourceService;
import com.yy.QSManage.service.FaSysEnumService;
import com.yy.QSManage.service.FaSysOptionService;
import com.yy.QSManage.service.base.impl.BaseServiceImpl;
import com.yy.QSManage.tool.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service
public class FaSysOptionServiceImpl extends BaseServiceImpl<FaSysOptionMapper, FaSysOption> implements FaSysOptionService {

    @Autowired
    FaSysOptionMapper optionMapper;
    @Autowired
    FaSysDataSourceService dataSourceService;
    @Autowired
    FaSysEnumService enumService;

    @Override
    public List<OptionsDto> getOptions(String id) {
        // 获取选项设置
        FaSysOption option = optionMapper.getOption(id);
        FaSysDataSource dataSource = dataSourceService.getById(option.getDataSourceId());
        if(dataSource.getType().equals("enum")){
            FaSysEnum sysEnum = enumService.getByParam(FaSysEnum::getCode, dataSource.getSource());
            String dataJson = sysEnum.getDataJson();
            return JSONUtil.toList(dataJson, OptionsDto.class);
        }else if(dataSource.getType().equals("api")){
            Method method = ReflectUtil.getMethod(FaSysOptionServiceImpl.class, dataSource.getSource());
            List<Object> objectList = ReflectUtil.invoke(this, method);
            if(objectList.isEmpty()){
                return null;
            }
            List<OptionsDto> options = new ArrayList<>();
            for (Object o : objectList) {
                String label = ReflectUtil.getFieldValue(o, option.getKeyField()).toString();
                String value = ReflectUtil.getFieldValue(o, option.getValueField()).toString();
                OptionsDto optionsDto = new OptionsDto();
                optionsDto.setLabel(label).setValue(value);
                options.add(optionsDto);
            }
            return options;
        }else{
            // 获取选项
            List<OptionsDto> options = optionMapper.getOptions(option);
            if(option.getTree().equals(1)){
                return TreeUtil.build(options);
            }
            return options;
        }
    }

    @Override
    public void ifExist(FaSysOption query) {
        LambdaQueryWrapper<FaSysOption> lambda = new LambdaQueryWrapper<>();
        lambda.eq(FaSysOption::getDataSourceId, query.getDataSourceId())
                .eq(FaSysOption::getKeyField, query.getKeyField())
                .eq(FaSysOption::getValueField, query.getValueField());
        FaSysOption one = getOne(lambda);
        if(Objects.nonNull(one)){
            throw new ServiceException("该选项已存在 ".concat(one.getName()));
        }
    }

    /**
     * api选项
     * @return
     */
    public List<OptionsDto> tables(){
        List<String> tables = dataSourceService.tables();
        List<OptionsDto> optionsDtos = new ArrayList<>();
        for (String table : tables) {
            optionsDtos.add(new OptionsDto().setLabel(table).setValue(table));
        }
        return optionsDtos;
    }
}
